# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=code-oss
pkgver=1.89.0
pkgrel=0
# get this from vscodium
_productjson=2faafce844f574e1db0a27def376bacad2a15023
pkgdesc="Visual Studio Code (OSS, with VSX)"
url="https://github.com/microsoft/vscode"
arch="aarch64 x86_64" # electron
license="MIT"
depends="electron ripgrep"
makedepends="
	electron-dev
	imagemagick
	jq
	krb5-dev
	libsecret-dev
	libxkbfile-dev
	nodejs18
	npm
	pngquant
	python3
	yarn
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/microsoft/vscode/archive/refs/tags/$pkgver.tar.gz
	product-$_productjson.json::https://github.com/VSCodium/vscodium/raw/$_productjson/product.json
	launcher
	enable-extensions.patch
	no-git.patch
	no-res.patch.noauto
	electron21.patch
	system-electron.patch.noauto
	no-source-maps.patch
	"
builddir="$srcdir/vscode-$pkgver"
options="!check net" # no tests (that make sense to run..)

export ELECTRON_SKIP_BINARY_DOWNLOAD=1
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
export npm_config_nodedir=/usr/include/electron/node_headers
export npm_config_build_from_source=true

prepare() {
	default_prepare

	sed -i '/^disturl\b/d' .yarnrc
	echo 'ignore-engines "true"' >> .yarnrc

	# block husky
	git init .

	# electron 20+ workaround for bad gyp usage
	# XXX: LARGEFILE64 fixme
	export CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE"
	export CPPFLAGS="$CPPFLAGS -D_LARGEFILE64_SOURCE"
	export CXXFLAGS="$CXXFLAGS -std=c++17 -D_LARGEFILE64_SOURCE"

	yarn install --cache-folder "$srcdir" --frozen-lockfile

	patch -Np1 < "$srcdir"/no-res.patch.noauto
	patch -Np1 < "$srcdir"/system-electron.patch.noauto

	# patch out telemetry
	# backported from https://github.com/VSCodium/vscodium/blob/master/undo_telemetry.sh
	rg --no-ignore -l "\.data\.microsoft\.com" . \
		| grep -v "\.map\$" | xargs -t -n 1 -P ${JOBS:-2} sed -i -E "s|//[^/]+\.data\.microsoft\.com|//0\.0\.0\.0|g"

	# merge the product.json from the repo with one from vscodium repo
	# this fixes some extensions, including python
	cp product.json original_product.json
	jq -s '.[0] * .[1]' original_product.json "$srcdir"/product-$_productjson.json > product.json
}

build() {
	node18 --max_old_space_size=4096 ./node_modules/.bin/gulp vscode-linux-x64-min

	cd resources/linux

	mv code.png code-1024-x.png
	local size
	for size in 16 24 32 48 64 128 192 256 512; do
		convert code-1024-x.png -resize ${size}x$size code-$size-x.png
		< code-$size-x.png pngquant --speed 1 - > code-$size.png
	done
	< code-1024-x.png pngquant --speed 1 - > code-1024.png
}

package() {
	mkdir -p "$pkgdir"/usr/share/applications
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code.desktop > "$pkgdir"/usr/share/applications/code-oss.desktop
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code-url-handler.desktop > "$pkgdir"/usr/share/applications/code-oss-url-handler.desktop

	for size in 16 24 32 48 64 128 192 256 512 1024; do
		install -Dm644 resources/linux/code-$size.png \
			"$pkgdir"/usr/share/icons/hicolor/${size}x$size/apps/com.visualstudio.code.oss.png
	done

	mkdir -p "$pkgdir"/usr/share/metainfo
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@LICENSE@@|MIT|g" \
		resources/linux/code.appdata.xml > "$pkgdir"/usr/share/metainfo/code-oss.appdata.xml

	mkdir -p "$pkgdir"/usr/share/mime/packages
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		resources/linux/code-workspace.xml > "$pkgdir"/usr/share/mime/packages/code-oss-workspace.xml

	mkdir -p "$pkgdir"/usr/share/bash-completion/completions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/bash/code > "$pkgdir"/usr/share/bash-completion/completions/code-oss

	mkdir -p "$pkgdir"/usr/share/zsh/site-functions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/zsh/_code > "$pkgdir"/usr/share/zsh/site-functions/_code-oss

	mkdir -p "$pkgdir"/usr/lib/code-oss/resources
	cp -a ../VSCode-linux-x64/resources/app "$pkgdir"/usr/lib/code-oss/resources/

	# disable update server
	sed -i "/updateUrl/d" "$pkgdir"/usr/lib/code-oss/resources/app/product.json

	# link to system rg
	ln -sfv /usr/bin/rg \
		"$pkgdir"/usr/lib/code-oss/resources/app/node_modules.asar.unpacked/@vscode/ripgrep/bin/rg

	install -Dm755 "$srcdir"/launcher "$pkgdir"/usr/bin/code-oss
}

sha512sums="
120178cabda6a22c87e989e15617a69d1665795a7796e1dea234455b235824b751ef45827522304ffa9813fe585b49985eb7011eaccd06b7e27e490691f25bb7  code-oss-1.89.0.tar.gz
c46cdcad59d796ea91492ee763897baaf36f7329126d6fd8f6bb782aaefeeba9ddf79c877ab8c22ad1f2194fee5b57b998b81ff994f714d36fc0759da8905ce2  product-2faafce844f574e1db0a27def376bacad2a15023.json
cca94a1762eec922b6fc4a93301087588455a82ea0e19733cb1067deec1fc64e05d466613860820d680dd8cf16e2aca30bc11afc84a315de1a9c2cb12854cbea  launcher
9f36c7fa6f0fd6a516f8e22c47f53013337985e59085bf1ea70165d42a513a92aa2a7a99ef0715e752c7190fd39ba703d405838e61bcfb60cabe47c421781eb4  enable-extensions.patch
4f10b14aaab71e69652662dea0dd5ba00d996282bc864743210185d93a9b7c54c71942fe1e7cd630abd914e3c770eca6c2110c13fb6a8c5e728c5172d1d0cd9f  no-git.patch
48e46bd2ec1490e456a9227c4d74eae5e5c6a7e48a98f8f463370bf60eb7fcb140337e3e02577dea1eabbc48174e2e3b31a4717a945d5dc21ccdbfd3a9807349  no-res.patch.noauto
050bcf858362836ad71ae204a50ce61eb1d512ae29778bff4f1f162a8650892e4a9d44295319d8142126a49a8fa1e6b306429c74a7448077051d479f04c19a93  electron21.patch
7808076aff7037369c9dd40ee07374311a954be513ae08b4c152ff04cbd6fc20ab9e9799e0b62da91135084f98dc55fbe0b593f4a263c30dd12fd88d680f3db0  system-electron.patch.noauto
20310187474b13bb0d4a7d39cdd93cdb005bf241110da0b02c4fa0927363fe8ea566dd259ce767ceffab771806f3365c209e401f1a3435361582bbedb525c5c7  no-source-maps.patch
"
